<?php
class ListCategoryForm extends Form{
	function ListCategoryForm(){
		Form::Form('ListCategoryForm');
		CGlobal::$website_title='Quản trị Danh mục';
	}

	function draw(){
								
		global $display;
		$this->beginForm();
		$sql="SELECT * FROM category ORDER BY position";
		
		$re=DB::query($sql);
		$items=array();
		$sub_items=array();
		$all_cats=array();
		
		if($re){
			$last_id=0;
			while ($row = mysql_fetch_assoc($re)){
				$all_cats[$row['id']] = $row;
				
				$row['image_src']='http://'.CGlobal::$img_server[$row['img_server']].'category/'.$row['id'].'.gif';	
				$row['image']=AZLib::getImageThumb('category/'.$row['id'].'.gif',50,50,0,$row['img_server']);	
				$row['del_image']=Url::build_current(array('cmd'=>'del_image','id'=>$row['id']));
				
				$row['edit_link']=Url::build_current(array('cmd'=>'edit','id'=>$row['id']));
				if($row['status']=='HOME'){
					$row['status']='<b>HOME</b> [ <a href="'.Url::build_current(array('cmd'=>'show','id'=>$row['id'])).'">Not set HOME</a> ] [ <a href="'.Url::build_current(array('cmd'=>'hide','id'=>$row['id'])).'">Ẩn Danh mục</a> ]';
				}
				elseif($row['status']=='SHOW'){
					$row['status']='SHOW [ <a href="'.Url::build_current(array('cmd'=>'set_home','id'=>$row['id'])).'">Set HOME</a> ] [ <a href="'.Url::build_current(array('cmd'=>'hide','id'=>$row['id'])).'">Ẩn Danh mục</a> ]';
				}
				elseif($row['status']=='HIDE'){
					$row['status']='<font color="#FF0000"><b>HIDE</b></font> [ <a href="'.Url::build_current(array('cmd'=>'set_home','id'=>$row['id'])).'">Set HOME</a> ] [ <a href="'.Url::build_current(array('cmd'=>'show','id'=>$row['id'])).'">Hiện Danh mục</a> ]';
				}
				$row['validate'] = '';
				
				if($row['parent_id']){//Là danh mục cấp 2 hoặc 3
					$sub_items[$row['parent_id']][$row['id']]=$row;
				}
				else{//Top cat
					if(!isset($sub_items[$row['id']]))
					$sub_items[$row['id']]=array();
					
					if($last_id){
						$items[$last_id]['down']='<a href="'.Url::build_current(array('cmd'=>'move_down','id'=>$last_id)).'"><img src="style/images/admin/down_arrow_.gif" /></a>';
						$row['up']='<a href="'.Url::build_current(array('cmd'=>'move_up','id'=>$row['id'])).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
					}
					else {
						$row['up']='';
					}
					$row['down']='';
					
					$items[$row['id']]=$row;
					$last_id=$row['id'];
				}
			}
			
			if($last_id && $items[$last_id]['position']>1){
				$items[$last_id]['up']='<a href="'.Url::build_current(array('cmd'=>'move_up','id'=>$last_id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
			}
		}
		
		foreach ($sub_items as $id=>$subs){
			if(isset($items[$id])){//Nếu tồn tại DM cha là danh mục cấp 1
				$last_id=0;
				
				foreach ($sub_items[$id] as $sub_id=>$sub){
					if($last_id){
						$sub_items[$id][$last_id]['down']='<a href="'.Url::build_current(array('cmd'=>'move_down','id'=>$last_id)).'"><img src="style/images/admin/down_arrow_.gif" /></a>';
						$sub_items[$id][$sub_id]['up']='<a  href="'.Url::build_current(array('cmd'=>'move_up','id'=>$sub_id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
					}
					else {
						$sub_items[$id][$sub_id]['up']='';
					}
					$row['down']='';
					
					$last_id=$sub_id;
				}
				
				if($last_id && $sub_items[$id][$last_id]['position']>1){
					$sub_items[$id][$last_id]['up']='<a href="'.Url::build_current(array('cmd'=>'move_up','id'=>$last_id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
				}
				
				$items[$id]['sub_cat'] = $sub_items[$id];
			}
			elseif(isset($all_cats[$id]) && $all_cats[$id]['parent_id']){//Nếu tồn tại DM cha là danh mục cấp 2
				$parent_id = $all_cats[$id]['parent_id'];
				
				if(isset($items[$parent_id])){
					if(!isset($items[$parent_id]['sub_cat'][$id])){
						$items[$parent_id]['sub_cat'][$id] = array('id'=>$id,'name'=>$all_cats[$id]['name']);
					}
					
					$last_id=0;
			
					foreach ($sub_items[$id] as $sub_id=>$sub){
						if($last_id){
							$sub_items[$id][$last_id]['down']='<a href="'.Url::build_current(array('cmd'=>'move_down','id'=>$last_id)).'"><img src="style/images/admin/down_arrow_.gif" /></a>';
							$sub_items[$id][$sub_id]['up']='<a  href="'.Url::build_current(array('cmd'=>'move_up','id'=>$sub_id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
						}
						else {
							$sub_items[$id][$sub_id]['up']='';
						}
						$row['down']='';
						
						$last_id=$sub_id;
					}
					
					if($last_id && $sub_items[$id][$last_id]['position']>1){
						$sub_items[$id][$last_id]['up']='<a href="'.Url::build_current(array('cmd'=>'move_up','id'=>$last_id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
					}
					
					$items[$parent_id]['sub_cat'][$id]['subcat'] = $sub_items[$id];
				}
				else{
					$items[$parent_id]=array('id'=>$parent_id,'name'=>'(Đã bị xóa)','sub_cat'=>array($id=>array('id'=>$id,'name'=>$all_cats[$id]['name'],'subcat'=>$subs)));
				}
			}
			else{
				$items[$id]=array('id'=>$id,'name'=>'(Đã bị xóa)','sub_cat'=>$subs);
			}
		}
		
		//echo "<pre>";
		//print_r($items);
		
		$display->add('items',$items);
		$display->add('hover','');
		$display->output('list');
		$this->endForm();
	}				
}
?>